<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 
 * 后台登陆控制器
 * @author 66diqiu-li
 *
 */
class Admin_Datan_module extends CI_Module {
    private $sid;
    private $adminid;
    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->adminid = $this->session->userdata('admin_user_id');
        if (!$this->adminid) {
            redirect(site_url('admin/index/login'));
        }
        $this->load->model('line.Site_view_model');
        $this->load->model('line.Line_view_model');
        $this->load->model('order.Ordertourist_model');
        $this->load->model('admin.Datan_model');
    }

    /**
     * 站点访问量统计
     */
    public function siteview(){
        $this->data['title'] = '访问量统计';
        $this->data['view'] = 'siteview_chart';

        $startdate = $this->input->get_post('start_date');
        $enddate = $this->input->get_post('end_date');
//        $type = $this->input->get_post('type');//views浏览量shares分享量adds添加量
//        $type_array = array('views'=>'网站访问浏览量','shares'=>'线路分享量','adds'=>'线路添加量');


        if(empty($enddate))$enddate = date('Y-m-d',strtotime("today"));
        if(empty($startdate))$startdate= date('Y-m-d',strtotime("-1 month"));
//        if(empty($type))$type = 'views';


        $condition['startdate'] = $startdate;
        $condition['enddate']   = $enddate;
//        $condition['type']      = $type;
        $this->data['condition'] = $condition;
//        $this->data['type_name'] = $type_array[$type];
        $starttime=strtotime($startdate);
        $endtime=strtotime($enddate);
        $end_day=round(($endtime-$starttime)/86400);
        $add = date('j',$starttime);
        $date_array = array();
        $data_views = $data_mviews = array();
        for($day=$add; $day<=$end_day+$add; $day++){
            //echo $day;
            $temp_date = date("Y-m-d",mktime(0,0,0,date('m',$starttime),$day,date('Y',$starttime)));//
            array_push($date_array,$temp_date);
        }
        //print_r($date_array);exit;
        foreach ($date_array as $k => $v) {
            $data_views[$k] = $this->Site_view_model->fetch_count_by_date($v, $date_array[$k+1],'views');
            $data_mviews[$k] = $this->Site_view_model->fetch_count_by_date($v, $date_array[$k+1],'mviews');
        }

        $this->data['date_array'] = $date_array;
        $this->data['views'] = $data_views;
        $this->data['mviews'] = $data_mviews;
        $this->data['index_url'] = site_url('admin/datan/siteview');
        $this->load->view('datan/siteview_chart',$this->data);

    }

    /**
     * 线路访问量前十名
     */
    function linerank(){
        $num = 10;
        $x_list = array();
        $y_list = array();

        $list = $this->Line_view_model->fetch_rank_list();
        for($i=1;$i<=$num;$i++){
            $x_list[$i] = $i;
            $y_list[$i] = empty($list[$i-1])?0:$list[$i-1]['visits'];
            $name_list[$i] = empty($list[$i-1])?'':$list[$i-1]['line_name'];
        }
        $data['x_list'] = $x_list;
        $data['y_list'] = $y_list;
        $data['name_list'] = $name_list;
        $data['list'] = $list;
        $data['title'] = '线路访问量排名';
        $this->load->view('datan/linerank_chart',$data);
    }

    /**
     * 客户年龄段分析
     */
    function tourist_age(){

        $y_list[0] = $this->Ordertourist_model->fetch_by_age(0,15);
        $y_list[1] = $this->Ordertourist_model->fetch_by_age(16,25);
        $y_list[2] = $this->Ordertourist_model->fetch_by_age(26,35);
        $y_list[3] = $this->Ordertourist_model->fetch_by_age(36,45);
        $y_list[4] = $this->Ordertourist_model->fetch_by_age(46,55);
        $y_list[5] = $this->Ordertourist_model->fetch_by_age(56,65);
        $y_list[6] = $this->Ordertourist_model->fetch_by_age(66,0);
        $data['y_list'] = $y_list;
        $data['x_list'] = array('15以下','16-25岁','26-35岁','36-45岁','46-55岁','56-65岁','65岁以上');
        $data['title'] = '线路访问量排名';
        $this->load->view('datan/tourage_chart',$data);

    }

    /**
     * 订单线路分析
     */
    function orderline_rank(){
        $startdate = $this->input->get_post('start_date');
        $enddate = $this->input->get_post('end_date');

        $condition['startdate'] = $startdate;
        $condition['enddate']   = $enddate;

        $data['condition'] = $condition;
        $data['index_url'] = site_url('admin/datan/orderline_rank');
        $data['list'] = $this->Datan_model->orderline_rank($startdate,$enddate);

        $this->load->view('datan/orderline_chart',$data);

    }

    function orderdest_rank(){
        $startdate = $this->input->get_post('start_date');
        $enddate = $this->input->get_post('end_date');

        $condition['startdate'] = $startdate;
        $condition['enddate']   = $enddate;

        $data['condition'] = $condition;
        $data['index_url'] = site_url('admin/datan/orderdest_rank');
        $data['list_1'] = $this->Datan_model->dest_rank($startdate,$enddate);
        $data['list_2'] = $this->Datan_model->dest_rank($startdate,$enddate,1);
        $data['x_list_1'] = $data['x_list_2'] = array();
        $data['y_list_1'] = $data['y_list_2'] = array();

        foreach($data['list_1'] as $key=>$val){
            $data['x_list_1'][$key] = $val['dest_name'];
            $data['y_list_1'][$key] = $val['num'];
        }
        foreach($data['list_2'] as $key=>$val){
            $data['x_list_2'][$key] = $val['dest_name'];
            $data['y_list_2'][$key] = $val['num'];
        }

        $this->load->view('datan/orderdest_chart',$data);
    }

    function tour_level(){
        $y_list[0] = $this->Datan_model->tour_level(0,200);
        $y_list[1] = $this->Datan_model->tour_level(201,500);
        $y_list[2] = $this->Datan_model->tour_level(501,1000);
        $y_list[3] = $this->Datan_model->tour_level(1001,2000);
        $y_list[4] = $this->Datan_model->tour_level(2001,5000);
        $y_list[5] = $this->Datan_model->tour_level(5001,8000);
        $y_list[6] = $this->Datan_model->tour_level(8000,10000);
        $y_list[7] = $this->Datan_model->tour_level(10000,0);
        $data['y_list'] = $y_list;
        $data['x_list'] = array('200以下','201-500','501-1000','1001-2000','2001-5000','5001-8000','8001-10000','10000以上');
        $data['title'] = '客户消费水平分析';
        $this->load->view('datan/tourlevel_chart',$data);
    }

    /**
     * 行程订单分析
     */
    function line_cat(){
        $y_list[0] = $this->Datan_model->line_cat(1);
        $y_list[1] = $this->Datan_model->line_cat(2);
        $y_list[2] = $this->Datan_model->line_cat(3);
        $x_list = array('国内游','周边游','出境游');
        $data['x_list'] = $x_list;
        $data['y_list'] = $y_list;
        $data['title']  = "行程订单分析";
//        print_r($data);exit;
        $this->load->view('datan/linecat_chart',$data);
    }

    /**
     * 公司业绩分析
     */
    function yeji(){

        $formate="'%Y-%m-%d'";

        $startdate = $this->input->get_post('start_date');
        $enddate = $this->input->get_post('end_date');
        $type = $this->input->get_post('type');
        $type = empty($type)?0:$type;
        $type_name = array('0'=>'总量','1'=>'线下','2'=>'PC端','6'=>'移动端');
        $data['title'] = '公司业绩分析-'.$type_name[$type];
        if(empty($enddate))$enddate = date('Y-m-d',strtotime("today"));
        if(empty($startdate))$startdate= date('Y-m-d',strtotime("-1 month"));


        $condition['startdate'] = $startdate;
        $condition['enddate']   = $enddate;
        $condition['type'] = $type;

        $data['condition'] = $condition;
        $starttime=strtotime($startdate);
        $endtime=strtotime($enddate);
        $end_day=round(($endtime-$starttime)/86400);
        $add = date('j',$starttime);
        $date_array = array();
        for($day=$add; $day<=$end_day+$add; $day++){
            $temp_date = date("Y-m-d",mktime(0,0,0,date('m',$starttime),$day,date('Y',$starttime)));
            array_push($date_array,$temp_date);
        }

        $where = " add_time>='".$startdate."' and add_time<='".$enddate." 23:59:59' ";
        if($type==6){
            $where.= " and cat_type=6 ";//移动端
        }
        if($type==2){
            $where.= " and cat_type in(2,4) ";//PC
        }
        if($type==1){
            $where.= " and cat_type not in(2,4,6) ";//线下订单
        }
        $list = $this->Datan_model->yeji($formate,$where);

        foreach ($date_array as $k => $v) {
            $y_list1[$k] = isset($list[$v])?$list[$v]['total_account']:0;//订单金额
            $y_list2[$k] = isset($list[$v])?$list[$v]['total_paid']:0;//已支付金额
        }

        $data['date_array'] = $date_array;
        $data['y_list1'] = $y_list1;
        $data['y_list2'] = $y_list2;
        $data['index_url'] = site_url('admin/datan/yeji');
        $this->load->view('datan/yeji_chart',$data);
    }

}